package com.c2b.algorithm.leetcode.base;

/**
 * <a href="https://leetcode.cn/problems/ransom-note/description/?envType=study-plan-v2&envId=top-interview-150">赎金信(Ransom Note)</a>
 * <p>给你两个字符串：ransomNote 和 magazine ，判断 ransomNote 能不能由 magazine 里面的字符构成。
 * <p>
 * 如果可以，返回 true ；否则返回 false 。
 * <p>
 * magazine 中的每个字符只能在 ransomNote 中使用一次。</p>
 * <p>
 * <b>示例</b>
 * <pre>
 * 示例 1：
 *      输入：ransomNote = "a", magazine = "b"
 *      输出：false
 *
 * 示例 2：
 *      输入：ransomNote = "aa", magazine = "ab"
 *      输出：false
 *
 * 示例 3：
 *      输入：ransomNote = "aa", magazine = "aab"
 *      输出：true
 *     </pre>
 * </p>
 * <p>
 * <b>提示：</b>
 * <ul>
 *     <li>1 <= ransomNote.length, magazine.length <= 10^5</li>
 *     <li>ransomNote 和 magazine 由小写英文字母组成</li>
 * </ul>
 * </p>
 *
 * @author c2b
 * @since 2023/10/11 11:09
 */
public class LC0383RansomNote_S {

    static class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            if (ransomNote.length() > magazine.length()) {
                return false;
            }
            int[] charCountArr = new int[26];
            for (char ch : magazine.toCharArray()) {
                charCountArr[ch - 'a'] += 1;
            }
            for (char ch : ransomNote.toCharArray()) {
                if (charCountArr[ch - 'a'] == 0) {
                    return false;
                }
                charCountArr[ch - 'a'] -= 1;
            }
            return true;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.canConstruct("a", "b"));
        System.out.println(solution.canConstruct("aa", "ab"));
        System.out.println(solution.canConstruct("aa", "aab"));
    }
}
